import requests
from lxml import etree
from bs4 import BeautifulSoup
import pymysql

#获取医院链接    
def get_hospital_codes(city):
	code_list = []
	url = 'https://dxy.com/health/hospital/location/{}0000'.format(city)
	headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'}
	htm = requests.get(url,headers=headers).text
	html = etree.HTML(htm)
	hospital_links = html.xpath('/html/body/div[@class="hospital-all__ly"]/div[@class="hospital-all__content hospital-more clearfix"]/div[@class="hospital-more__content "]/ul[@class="content-region j-hospital-region "]/li[@class="clearfix"]/ul/li/a/@href')
	for link in hospital_links:
		code_list.append(link.replace('/health/hospital/',''))
	return code_list
#获取医院信息
def get_hospital_info(code):
	department_list = []
	url = 'https://dxy.com/health/hospital/{}'.format(str(code))
	headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'}
	htm = requests.get(url,headers=headers).text
	soup = BeautifulSoup(htm,'lxml')
	try:
		hospital_name = soup.select(' h1 ')[0].get_text().replace('\n','').replace(' ','')   #医院名称
		hospital_grade = soup.select(' .info ')[0].get_text().replace('\n','').replace(' ','')   #医院等级
		hospital_introduction = soup.select(' .item ')[0].get_text().replace('\n','').replace(' ','').replace('\\xa0','').replace('\\u3000','')   #简介
		hospital_address = soup.select(' .hospital-basic__info ')[0].get_text().replace('\n','').replace(' ','')   #地址
		hospital_phone = soup.select(' .hospital-basic__info ')[1].get_text().replace('\n','').replace(' ','')
		page = 0
		hospital_good = soup.select(' .hospital-detail-intro ')[0].get_text().replace('\n','').replace(' ','')   #擅长
		while 1:
			page += 1
			department_url = 'https://dxy.com/view/i/hospital/section/list?page_index={}&items_per_page=20&id=13744'.format(str(page))
			try:
				department_htm = requests.get(department_url).json().get('data').get('items')
				for department in department_htm:
					department_list.append(department.get('name'))
			except:
				break
		return hospital_name,hospital_grade,hospital_introduction,hospital_address,hospital_phone,hospital_good,str(department_list).replace('[\'','').replace('\']','').replace(' ','').replace('\',\'','|')
	except:
		pass
def get_save(info):
	try:
		cur.execute("INSERT into hospital (hospital_name,hospital_grade,hospital_introduction,hospital_address,hospital_phone,hospital_good,hospital_department) values (%s,%s,%s,%s,%s,%s,%s)",(info[0],info[1],info[2],info[3],info[4],info[5],info[6]))
		conn.commit()
		print(info[0])
	except:
		pass
if __name__ == '__main__':
	conn = pymysql.connect(host="localhost",user="root",passwd="111111",db="hospital",charset="utf8")
	cur = conn.cursor()
	cur.execute("use hospital")
	conn.commit()
	city_list = ['71', '82', '50', '31', '22', '62', '12', '42', '43', '37', '11', '34', '13', '63', '54', '64', '14', '61', '36', '51', '33', '35', '45', '23', '52', '32', '46', '15', '44', '21', '41', '81', '53', '65']
	for city in city_list:
		for code in get_hospital_codes(city):
			info = get_hospital_info(code)
			get_save(info)
	conn.close()
	cur.close()	

